<?php
	class JugadorsClass
	{
		public $nom;
		public $contrassenya;
		public $email;
		public $registre;
		public $nivell;
		public $distancia;
		
		private $connexio;

		public function __construct()
		{
			$this->nom = null;
			$this->contrassenya = null;
			$this->email = null;
			$this->registre = null;
			$this->nivell = null;
			$this->distancia = null;
			
			$this->connexio = new ConnexioBDclass();
		}
		
		public function Create()
		{
			$query = "INSERT INTO jugadors (nom, contrassenya, email, registre, nivell, distancia) VALUES ('".$this->nom."', '".$this->contrassenya."', '".$this->email."', '".$this->registre."', '".$this->nivell."', '".$this->distancia."');";
			$result = mysql_query($query);
			
			return $result;
		}
		
		public function Update()
		{
			$query = "UPDATE jugadors SET 
						contrassenya = ".$this->contrassenya.",
						email = ".$this->email.",
						registre = ".$this->registre.",
						nivell = ".$this->nivell.",
						distancia = ".$this->distancia." WHERE nom = ".$this->nom.";";
			
			$result = mysql_query($query);
			return $result;
		}
		
		public function Delete()
		{
			$query = "DELETE FROM jugadors WHERE nom = ".$this->nom.";";
			$result = mysql_query($query);
			return $result;
		}
		
		public function Filter($conditions, $order, $limit_start, $limit_end)
		{
			$result = array();
			$query = "SELECT * FROM jugadors";
			
			if($conditions != null)
			{
				$query .= " WHERE 1 = 1";
				foreach ($conditions as $value)
				{
					if ($value[1] == "OR") 
			  		{
			    		$query.=" AND ((".$value[0].") OR (".$value[2]."))";
			  		}
			  		else
			  		{
			    		$query.=" AND ".$value[0]." ".$value[1]." '".$value[2]."'";
			  		}
				}
				if($order!=null)
        		{
          			$query.=" ORDER BY ";
		  			$coma="";
          			foreach ($order as $value)
          			{
              			$query.=$coma.$value[0]." ".$value[1]; // order_field, order direction ( data_inici ASC)
			  			$coma=",";
          			}
        		}
			}
			if ($limit_end!=null)
			{
          		$query.=" limit ".$limit_start.",".$limit_end;
        	}

		}
	}
?>